package com.app.util.total;

import java.lang.reflect.Method;
import java.util.Collection;

public class SumUtil<T> {

	public void sumList(T t, Collection<T> list) {
		if (null == list)
			return;
		if (null == t)
			return;
		for (T _t : list) {
			add(t, _t);
		}
	} 

	private void add(T sum, T t) {
		Method[] methods = sum.getClass().getMethods();
		for (Method set : methods) {
			String mname = set.getName();
			if (mname.length() <= 3) {
				continue;
			}
			String first = mname.substring(0, 3);
			String end = mname.substring(3);
			if (first.equals("set")) {
				try {
					Method get = sum.getClass().getMethod("get" + end, new Class[] {});
					Object tValue = get.invoke(t, new Object[] {});
					if (null == tValue) {
						return;
					}
					Object sumValue = get.invoke(sum, new Object[] {});
					if (tValue instanceof Short || short.class.isInstance(tValue)) {
						if (sumValue == null) {
							sumValue = (Short) tValue;
						} else if (tValue != null) {
							sumValue = (short) (Short.valueOf(tValue.toString()) + Short.valueOf(sumValue.toString()));
						}
					}
					if (tValue instanceof Integer || int.class.isInstance(tValue)) {
						if (sumValue == null) {
							sumValue = (Integer) tValue;
						} else if (tValue != null) {
							sumValue = Integer.valueOf(tValue.toString()) + Integer.valueOf(sumValue.toString());
						}
					}
					if (tValue instanceof Long || long.class.isInstance(tValue)) {
						if (sumValue == null) {
							sumValue = (Long) tValue;
						} else if (tValue != null) {
							sumValue = Long.valueOf(tValue.toString()) + Long.valueOf(sumValue.toString());
						}
					}
					if (tValue instanceof Float || float.class.isInstance(tValue)) {
						if (sumValue == null) {
							sumValue = (Float) tValue;
						} else if (tValue != null) {
							sumValue = Float.valueOf(tValue.toString()) + Float.valueOf(sumValue.toString());
						}
					}
					if (tValue instanceof Double || double.class.isInstance(tValue)) {
						if (sumValue == null) {
							sumValue = (Double) tValue;
						} else if (tValue != null) {
							sumValue = Double.valueOf(tValue.toString()) + Double.valueOf(sumValue.toString());
						}
					}
					set.invoke(sum, new Object[] { sumValue });
				} catch (Exception e) {
				}
			}
		}
	}

	public static void main(String[] args) {
	}

}
